/**
 * 
 */
jQuery(function($){

	// Number of columns changed
	$('#number-columns').change(function(){		
		//alert($(this).children('option:selected').val()); 
		var column=$(this).children('option:selected').val();
		$('#feature-columns')[0].className = $('#feature-columns')[0].className.replace(/\bfeature-columns-.[0-9]?\b/g, '');
		
		$('#feature-columns').addClass('feature-columns-' + column);		
	}); 

	// The column count
	var iCol = $('.feature-columns .column').not('.addnew').length;
	
	/**
	 * Insert clearing divs in the price columns to put them into neat rows.
	 */
	resetColumns = function(){
		$('.feature-columns').sortable('refresh');
		var currentTop = $('.feature-columns .column').position().top;
		$('.feature-columns .clear').remove();
		$('.feature-columns .column').each(function(i, el){
			var $$ = $(el);
			
			// Reset the column names
			$$.find('.column-title').attr('name', 'feature_'+i+'_title');
			$$.find('.column-imageurl').attr('name', 'feature_'+i+'_imageurl');
			$$.find('.column-linkurl').attr('name', 'feature_'+i+'_linkurl');			
			$$.find('.column-description').attr('name', 'feature_'+i+'_description');
						
			if($$.position().top != currentTop){
				$$.before($('<div class="clear"></div>'));
				currentTop = $$.position().top;
			}
			
			$$.sortable('refresh');
		});
	};
	
	var addPriceCol = function(){
		var thisICol = iCol++;
		var column = $('#column-skeleton').clone().attr('id', null);
		
		// Set up the new column
		$('.column-title',column).attr('name', 'feature_'+thisICol+'_title').val('');
		$('.column-imageurl',column).attr('name', 'feature_'+thisICol+'_imageurl').val('');
		$('.column-linkurl',column).attr('name', 'feature_'+thisICol+'_linkurl').val('');
		$('.column-description',column).attr('name', 'feature_'+thisICol+'_description').val('');
		
		//$('.type input', column).attr({
		//	id : 'price_recommend_'+thisICol,
		//	value : thisICol
		//});
		//$('.type label', column).attr('for', 'price_recommend_'+thisICol);
		
		// And set up the first feature
		column.css('display', 'block').addClass('column').insertBefore('.feature-columns .addnew');
		
		// Remove the placeholder column
		//$('.feature', column).remove();
		
		// Make the pricetable features sortable
		//column.find('.feautres').sortable({
		//	'items' : '.feature',
		//	'handle' : '.feature-handle',
		//	'stop' : resetColumns,
		//	'opacity' : 0.6
		//});
		
		// Column deletion
		column.find('> a.deletion').click(function(){
			if(confirm(pt_messages.delete_column)){
				column.remove();
				resetColumns();
			}
			return false;
		});
		
	/*	$('a.addfeature', column).click(function(){
			var featureCount = $('.feautres .feature', column).length;
			var feature = $('#column-skeleton .feature').last().clone().appendTo($('.feautres', column));
			
			feature.find('.feature-title').attr('name', 'price_'+thisICol+'_feature_'+featureCount+'_title');
			feature.find('.feature-sub').attr('name', 'price_'+thisICol+'_feature_'+featureCount+'_sub');
			feature.find('.feature-description').attr('name', 'price_'+thisICol+'_feature_'+featureCount+'_description').elastic();
			
			feature.find('> a.deletion').click(function(){
				if(confirm(pt_messages.delete_feature)){
					feature.slideUp('normal',feature.remove());
					resetColumns();
				}
				return false;
			});
			
			// Reset columns to trigger the drag and drop
			resetColumns();
			
			return false;
		}).click();
		*/
		column.find('input').placeholder();
		
		// Reset the positions of the columns
		resetColumns();
	}
	
	$('.feature-columns .addnew').click(function(){
		addPriceCol();
		return false;
	});
	
	// The price column sortable
	$('.feature-columns').sortable({
		'items' : '.column:not(.addnew)',
		'handle' : '.column-handle',
		'stop' : resetColumns,
		'opacity' : 0.6
	});
	
	// Set up the existing price columns
	$('.feature-columns .column').not('.addnew').each(function(i, el){
		var thisICol = i;
		var column = $(el);
		
		// Column deletion
		column.find('> a.deletion').click(function(){
			if(confirm(pt_messages.delete_column)){
				column.remove();
				resetColumns();
			}
			return false;
		});
		/*
		column.find('a.addfeature').click(function(){
			var featureCount = $('.feautres .feature', column).length;
			var feature = $('#column-skeleton .feature').last().clone().appendTo($('.feautres', column));
			
			feature.find('.feature-title').attr('name', 'price_'+thisICol+'_feature_'+featureCount+'_title');
			feature.find('.feature-sub').attr('name', 'price_'+thisICol+'_feature_'+featureCount+'_sub');
			feature.find('.feature-description').attr('name', 'price_'+thisICol+'_feature_'+featureCount+'_description');
			
			feature.find('> a.deletion').click(function(){
				if(confirm(pt_messages.delete_feature)){
					feature.slideUp('normal',feature.remove());
					resetColumns();
				}
				return false;
			});
			
			// Set up auto resizing
			return false;
		});
		*/
		column.find('input').placeholder();
		/*
		// Make the pricetable elements sortable
		column.find('.feautres').sortable({
			'items' : '.feature',
			'handle' : '.feature-handle',
			'stop' : resetColumns,
			'opacity' : 0.6
		});
		
		column.find('.feature').each(function(){
			var feature = $(this);
			feature.find('> a.deletion').click(function(){
				if(confirm(pt_messages.delete_feature)){
					feature.slideUp('normal',feature.remove());
					resetColumns();
				}
				return false;
			});
		});
		*/
	});
    /*
    // Make the recommended price table uncheckable
    $('.feature-columns input[name="price_recommend"]')
        .change(function(){
            $(this).data('changed', true);
        })
        .click(function(){
            var $$ = $(this);
            var changed = $$.data('changed');
            
            if((changed == undefined || changed == false) && $$.is(':checked')){
                $$.prop('checked', false);
            }
            
            $$.data('changed', false);
        });
	*/
	// Set up auto resizing on existing textareas
	$('.feature-columns textarea.autoresize').not('#column-skeleton textarea.autoresize').elastic().trigger('change');
	
	// Reset the columns to start
	resetColumns();
	$(window).resize(resetColumns);	
});